package com.ruoyi.listener.improve;

import com.ruoyi.quality.domain.bo.QualityContinuousImprovementBo;
import com.ruoyi.quality.domain.vo.QualityContinuousImprovementVo;
import com.ruoyi.quality.service.IQualityContinuousImprovementService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Component
public class SharingListener implements ExecutionListener {
    @Resource
    public IQualityContinuousImprovementService improvementService;

    private static SharingListener sharingListener;

    @PostConstruct
    public void init() {
        sharingListener = this;
        sharingListener.improvementService = this.improvementService;
    }

    @Override
    public void notify(DelegateExecution delegateExecution) {
        String procId = delegateExecution.getProcessInstanceId();
        QualityContinuousImprovementVo vo = sharingListener.improvementService.queryByProcessInstanceId(procId);
        QualityContinuousImprovementBo bo = new QualityContinuousImprovementBo();
        BeanUtils.copyProperties(vo,bo);
        Integer totalScore = bo.getTotalScore();
        Integer review = bo.getReview();
        Integer sharing = totalScore/review;
        delegateExecution.setVariable("score",sharing);
        bo.setSharing(sharing);
        sharingListener.improvementService.updateByBo(bo);
    }
}
